﻿Public Class VM_EmergencyContactInformation
	Implements IDisposable

	Dim workingPublisher As EmergencyContactData
	Dim db As New CongregationDataContainerLocal

	Public ReadOnly Property FullName As String
		Get
			'Using db As New CongregationDataContainerLocal
			Return db.PublisherDatas.SingleOrDefault(Function(x) x.PublisherID = workingPublisher.PublisherID).FullName
			'End Using
		End Get
	End Property

	Public Property HomeTelephone As String
		Get
			Return workingPublisher.HomeTelephone
		End Get
		Set(value As String)
			workingPublisher.HomeTelephone = value
			db.SaveChanges()
		End Set
	End Property

	Public Property WorkTelephone As String
		Get
			Return workingPublisher.WorkTelephone
		End Get
		Set(value As String)
			workingPublisher.WorkTelephone = value
			db.SaveChanges()
		End Set
	End Property

	Public Property MobileTelephone As String
		Get
			Return workingPublisher.PublisherData.MobileTelephone
		End Get
		Set(value As String)
			workingPublisher.PublisherData.MobileTelephone = value
			db.SaveChanges()
		End Set
	End Property

	Public Property PhysicalAddress As String
		Get
			Return workingPublisher.PhysicalAddress
		End Get
		Set(value As String)
			workingPublisher.PhysicalAddress = value
			db.SaveChanges()
		End Set
	End Property

	Public Property Email As String
		Get
			Return workingPublisher.PublisherData.Email
		End Get
		Set(value As String)
			workingPublisher.PublisherData.Email = value
			db.SaveChanges()
		End Set
	End Property

	Public Property SpecialNeeds As String
		Get
			Return workingPublisher.SpecialNeeds
		End Get
		Set(value As String)
			workingPublisher.SpecialNeeds = value
			db.SaveChanges()
		End Set
	End Property

	Public Property OtherName As String
		Get
			Return workingPublisher.OtherName
		End Get
		Set(value As String)
			workingPublisher.OtherName = value
			db.SaveChanges()
		End Set
	End Property

	Public Property OtherHomeTelephone As String
		Get
			Return workingPublisher.OtherHomeTelephone
		End Get
		Set(value As String)
			workingPublisher.OtherHomeTelephone = value
			db.SaveChanges()
		End Set
	End Property

	Public Property OtherWorkTelephone As String
		Get
			Return workingPublisher.OtherWorkTelephone
		End Get
		Set(value As String)
			workingPublisher.OtherWorkTelephone = value
			db.SaveChanges()
		End Set
	End Property

	Public Property OtherMobileTelephone As String
		Get
			Return workingPublisher.OtherMobileTelephone
		End Get
		Set(value As String)
			workingPublisher.OtherMobileTelephone = value
			db.SaveChanges()
		End Set
	End Property

	Public Property OtherEmail As String
		Get
			Return workingPublisher.OtherEmail
		End Get
		Set(value As String)
			workingPublisher.OtherEmail = value
			db.SaveChanges()
		End Set
	End Property

	Public Sub New(FamilyHeadID As Long)

		workingPublisher = db.EmergencyContactDatas.SingleOrDefault(Function(x) x.PublisherID = FamilyHeadID)

		If workingPublisher Is Nothing Then
			workingPublisher = New EmergencyContactData
			workingPublisher.PublisherID = FamilyHeadID
			db.EmergencyContactDatas.AddObject(workingPublisher)
			db.SaveChanges()
		End If
	End Sub

#Region "IDisposable Support"
	Private disposedValue As Boolean ' To detect redundant calls

	' IDisposable
	Protected Overridable Sub Dispose(disposing As Boolean)
		If Not Me.disposedValue Then
			If disposing Then
				' TODO: dispose managed state (managed objects).
				db.Dispose()
			End If

			' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
			' TODO: set large fields to null.
		End If
		Me.disposedValue = True
	End Sub

	' TODO: override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources.
	'Protected Overrides Sub Finalize()
	'    ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
	'    Dispose(False)
	'    MyBase.Finalize()
	'End Sub

	' This code added by Visual Basic to correctly implement the disposable pattern.
	Public Sub Dispose() Implements IDisposable.Dispose
		' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
		Dispose(True)
		GC.SuppressFinalize(Me)
	End Sub
#End Region

End Class
